package fhq.hcmute.edu.vn.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import fhq.hcmute.edu.vn.config.ListDevicesConnected;
import fhq.hcmute.edu.vn.language.Language;
import fhq.hcmute.edu.vn.language.LanguageFactory;
import fhq.hcmute.edu.vn.network.PhoneInfos;

public class ListDevicesDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8217574807429740364L;
	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private ArrayList<PhoneInfos> arrayList;
	private DefaultTableModel dtm;
	private Language language;

	/**
	 * Create the dialog.
	 */
	public ListDevicesDialog(final Frame parent, final boolean modal) {
		super(parent, modal);
		language = LanguageFactory.getInstance().getLanguage();
		setBounds(100, 100, 544, 247);
		getContentPane().setLayout(new BorderLayout());
		setCenterScreen();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 503, 145);
		contentPanel.add(scrollPane);
		table = new JTable();
		arrayList = ListDevicesConnected.getListDataConnections();
		if (arrayList == null) {
			arrayList = new ArrayList<>();
		}
		configJTable(arrayList);
		scrollPane.setViewportView(table);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(10, 156, 503, 42);
		contentPanel.add(panel);

		JButton btnRemove = new JButton(language.btnRemove());

		btnRemove.addActionListener(onClickDelete);
		btnRemove.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnRemove.setActionCommand("OK");
		btnRemove.setBounds(127, 5, 100, 35);
		panel.add(btnRemove);

		JButton btnExit = new JButton(language.btnExit());
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				dispose();
			}
		});
		btnExit.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnExit.setActionCommand("Cancel");
		btnExit.setBounds(280, 5, 100, 35);
		panel.add(btnExit);
	}

	private ActionListener onClickDelete = new ActionListener() {
		public void actionPerformed(final ActionEvent arg0) {

			int result = JOptionPane.showConfirmDialog(ListDevicesDialog.this,
					language.listDevicesDialogDeleteTitle(),
					language.listDevicesDialogDeleteConfirm(),
					JOptionPane.YES_NO_OPTION);
			if (result == JOptionPane.YES_OPTION) {
				DefaultTableModel model = (DefaultTableModel) table.getModel();
				int[] rows = table.getSelectedRows();
				for (int i = 0; i < rows.length; i++) {
					model.removeRow(rows[i] - i);
					ListDevicesConnected.removeDataConnections(rows[i] - i);
				}
				try {
					ListDevicesConnected.commit();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	};

	private void configJTable(final ArrayList<PhoneInfos> arrayList) {
		dtm = new DefaultTableModel() {
			private static final long serialVersionUID = -1492154389291323051L;

			@Override
			public boolean isCellEditable(final int row, final int column) {
				return false;
			}
		};
		dtm.addColumn(language.listDevicesDialogTableID());
		dtm.addColumn(language.listDevicesDialogTableModel());
		dtm.addColumn(language.listDevicesDialogTableDeviceName());
		dtm.addColumn(language.listDevicesDialogTableIMEINumber());
		dtm.setRowCount(arrayList.size());
		for (int i = 0; i < arrayList.size(); i++) {
			PhoneInfos phoneInfos = arrayList.get(i);
			dtm.setValueAt(i + 1, i, 0);
			dtm.setValueAt(phoneInfos.getDeviceModelNumber(), i, 1);
			dtm.setValueAt(phoneInfos.getDeviceName(), i, 2);
			dtm.setValueAt(phoneInfos.getPhoneIMEI(), i, 3);
		}
		table = new JTable();
		table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
		table.setModel(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment(JLabel.CENTER);
		table.getColumnModel().getColumn(0).setPreferredWidth(70);
		table.getColumnModel().getColumn(0).setCellRenderer(rightRenderer);
		table.getColumnModel().getColumn(1).setPreferredWidth(80);
		table.getColumnModel().getColumn(1).setCellRenderer(rightRenderer);
		table.getColumnModel().getColumn(2).setPreferredWidth(230);
		// table.getColumnModel().getColumn(2).setCellRenderer(rightRenderer);
		table.getColumnModel().getColumn(3).setPreferredWidth(120);
		table.getColumnModel().getColumn(3).setCellRenderer(rightRenderer);
		// table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}

	private void setCenterScreen() {
		int width = this.getWidth();
		int height = this.getHeight();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screenSize.width / 2) - (width / 2);
		int y = (screenSize.height / 2) - (height / 2);
		this.setLocation(x, y);
	}
}
